<?php
	header('content-type:text/html;charset=utf-8');
class MenuAction extends Action {
	public function index(){
		if(isset($_SESSION['uid'])){
			$m=M('Menu');
			$data=$m->order('id')->getField('id,title,status,ordering');
			$this->assign('data',$data);

			$c=M('content');
			
			$count=array('');
			for($i=1;$i<=$c->count();$i++){
				$where['menuid']=$i;
				$count[]=$c->where($where)->getField('count(*)');
			}
			
			$this->assign('flag','index');
			$this->assign('count',$count);
			$this->display();
		}
	}

	public function create(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		$m=M('Menu');
		$max=$m->max('id');
		$this->assign('max',$max+1);
		$ordering=$m->order('id')->getField('title,ordering');
		$this->assign('ordering',$ordering);
		$count=$m->count()+1;
		$this->assign('count',$count);
		$this->display();
	}
	public function addmenu(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		if ($this->isPost()){
			$m=M('Menu');
			$a=$m->create();
			$m->ordering+=1;
			if($m->add()){
				$this->success('添加成功','index');
			}else{
				$this->error('添加失败');
			}
		}
	}
	
	public function delete(){
		if (!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		if ($this->isPost()) {
			$arr=$this->_post('menubox');
			$m=M('Menu');
			for($i=0;$i<count($arr);$i++){
				$where['id']=$arr[$i];
				$m->where($where)->delete();
			}
			$this->success('删除成功','index');
		}
		
		
	}
	public function edit(){
		if(!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		if($this->isGet()){
			$m=M('Menu');
			$id=$this->_get('id');
			$mid=$this->_get('mid');
			$this->assign('count',$id);
			$where['id']=$mid;
			$data=$m->where($where)->find();
			$ordering=$m->getField('title,ordering');
			$this->assign('ordering',$ordering);
			//dump($data);
			$this->assign('data',$data);
			$this->display('update');
		}
	}
	public function update(){
		if (!isset($_SESSION['uid'])){
			$this->redirect('Index/index');
		}
		if ($this->isPost()) {
			$m=M('Menu');
			$m->create();
			$m->save() or $this->error('失败');
			$this->success('成功','index');
		}
	}
}
?>